/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.base;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.base.adapter.BaseGeneralRecyclerAdapter;
import com.dataoke.shoppingguide.app773026.base.adapter.BaseRecyclerAdapter;
import com.dataoke.shoppingguide.app773026.base.fragments.BaseFragment;
import com.dataoke.shoppingguide.app773026.widget.RecyclerRefreshLayout;

import java.util.Date;
import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public abstract class BaseRecyclerFragment<Presenter extends BaseListPresenter, Model> extends BaseFragment
        implements BaseListView<Presenter, Model>,
        BaseRecyclerAdapter.OnItemClickListener,
        RecyclerRefreshLayout.SuperRefreshLayoutListener,
        BaseGeneralRecyclerAdapter.Callback {
    protected RecyclerRefreshLayout mRefreshLayout;
    protected RecyclerView mRecyclerView;
    protected BaseRecyclerAdapter<Model> mAdapter;
    protected Presenter mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_recycler;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mRefreshLayout = mRoot.findViewById(R.id.refreshLayout);
        mRefreshLayout.setSuperRefreshLayoutListener(this);
        mRecyclerView = mRoot.findViewById(R.id.recyclerView);
        initHeader();
        mAdapter = getAdapter();
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);
        hokeSetHeaderView();
    }

    protected void initHeader() {

    }


    protected void hokeSetHeaderView() {

    }

    @Override
    protected void initData() {
        super.initData();
        mRefreshLayout.post(() -> {
            mRefreshLayout.setRefreshing(true);
            if (mPresenter == null)
                return;
            mPresenter.onRefreshing();
        });
    }

    @Override
    public void onItemClick(int position) {
        Model model = mAdapter.getItem(position);
        if (model != null)
            onItemClick(model, position);
    }

    @Override
    public void onRefreshing() {
        if (mPresenter == null)
            return;
        mAdapter.setState(BaseRecyclerAdapter.STATE_HIDE, true);
        mPresenter.onRefreshing();
    }

    @Override
    public void onLoadMore() {
        mAdapter.setState(BaseRecyclerAdapter.STATE_LOADING, true);
        mPresenter.onLoadMore();
    }

    @Override
    public void onScrollToBottom() {

    }

    @Override
    public void onRefreshSuccess(List<Model> data) {
        mAdapter.resetItem(data);
    }

    @Override
    public void onLoadMoreSuccess(List<Model> data) {
        mAdapter.addAll(data);
    }

    @Override
    public void showNotMore() {
        mAdapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
    }

    @Override
    public void showNetworkError(int strId) {
        mAdapter.setState(BaseRecyclerAdapter.STATE_INVALID_NETWORK, true);
        mAdapter.setState(BaseRecyclerAdapter.STATE_LOAD, true);
    }

    @Override
    public void onComplete() {
        mRefreshLayout.onComplete();
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.mPresenter = presenter;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    public Date getSystemTime() {
        return new Date();
    }

    protected abstract BaseRecyclerAdapter<Model> getAdapter();

    protected abstract void onItemClick(Model model, int position);
}
